﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BookMS
{
    public partial class admin22 : Form
    {
        string ID = "";
        public admin22()
        {
            InitializeComponent();
        }
        public admin22(string id,string name,string author,string press,string number)
        {
            InitializeComponent();
            ID = textBox1.Text = id;
            textBox2.Text = name;
            textBox3.Text = author;
            textBox4.Text = press;
            textBox5.Text = number;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "" && textBox4.Text != "" && textBox5.Text != "")
            {
                Dao dao = new Dao();
                string str = $"update book set id='{textBox1.Text}',name='{textBox2.Text}',author='{textBox3.Text}',press='{textBox4.Text}',number={textBox5.Text} where id={ID}";
                try
                {
                    int n = dao.Execute(str);
                    if (n > 0)
                    {
                        MessageBox.Show("修改成功！");
                    }
                    else
                    {
                        MessageBox.Show("修改失败！");
                    }
                }
                catch (MySql.Data.MySqlClient.MySqlException ex)
                {
                    // 检查是否是主键重复的异常
                    if (ex.Number == 1062)  // 1062: Duplicate entry for key 'PRIMARY'
                    {
                        MessageBox.Show("修改失败：书号已经存在！");
                    }
                    else
                    {
                        MessageBox.Show($"请输入正确的数据！");
                    }
                }
                dao.DaoClose();
            }
            else
            {
                MessageBox.Show("输入不允许有空！");
            }
        }
    }
}
